package com.zbkj.admin.controller;

import com.zbkj.common.model.card.StoreGiftCardUser;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.StoreCouponUserSearchRequest;
import com.zbkj.common.request.StoreGiftCardUserSearchRequest;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.response.StoreCouponUserResponse;
import com.zbkj.common.response.StoreGiftCardUserResponse;
import com.zbkj.service.service.StoreGiftCardUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Swallows
 * @date 2022-08-22 09:32
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("api/admin/marketing/giftCard/user")
@Api(tags = "营销 -- 礼品卡 -- 领取记录")
public class StoreGiftCardUserController {

    private final StoreGiftCardUserService giftCardUserService;

    /**
     * 分页显示礼品卡发放记录表
     * @param request 搜索条件
     * @param pageParamRequest 分页参数
     */
    @PreAuthorize("hasAuthority('admin:giftCard:user:list')")
    @ApiOperation(value = "分页列表")
    @GetMapping("/list")
    public CommonResult<CommonPage<StoreGiftCardUserResponse>> getList(@Validated StoreGiftCardUserSearchRequest request, @Validated PageParamRequest pageParamRequest) {
        CommonPage<StoreGiftCardUserResponse> cardUserResponseCommonPage = CommonPage.restPage(giftCardUserService.getPageList(request, pageParamRequest));
        return CommonResult.success(cardUserResponseCommonPage);
    }

    /**
     * 后台批量发卡
     *
     * @param uid 用户id
     * @param cardUserList 发放礼品卡列表
     */
    @PostMapping("/deliver")
    @ApiOperation("发放礼品卡")
    @PreAuthorize("hasAuthority('admin:giftCard:user:deliver')")
    public CommonResult<String> deliver(@RequestParam String uid, @RequestParam String cardUserList) {

        if (giftCardUserService.deliver(uid, cardUserList)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }
}
